package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.DelayChangeTeacher;
import com.yuyou.fn.educational.mapper.DelayChangeTeacherMapper;
import com.yuyou.fn.educational.service.IDelayChangeTeacherService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.vo.DelayChangeTeacherVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * DelayChangeTeacher 表数据服务层接口实现类
 *
 */
@Service
public class DelayChangeTeacherServiceImpl extends SuperServiceImpl<DelayChangeTeacherMapper, DelayChangeTeacher,Long> implements IDelayChangeTeacherService {

    @Resource
    private ITeacherService teacherService;

    @Override
    @Transactional
    public void saveDelayUpdateTeacher(Long teacherId,List<DelayChangeTeacherVo> delayChangeTeacherVoList) {

        if (teacherId==null || delayChangeTeacherVoList==null ){
            throw new BusException("缺乏改变的信息");
        }
        Principals.Principal principal=Principals.get();
        if (principal==null){
            throw new BusException("缺乏操作人");
        }
        List<DelayChangeTeacher> delayChangeTeachers=new ArrayList<>();
        for (DelayChangeTeacherVo delayChangeTeacherVo : delayChangeTeacherVoList) {
            if (delayChangeTeacherVo.getChangeType()==null || delayChangeTeacherVo.getHappenDate()==null || StringUtils.isEmpty(delayChangeTeacherVo.getChangeData())){
                throw new BusException("缺乏必要信息");
            }
            if ( !(delayChangeTeacherVo.getChangeType()==1 || delayChangeTeacherVo.getChangeType()==2)){
                throw new BusException("请传入正确字段编码");
            }

            if (delayChangeTeacherVo.getHappenDate().compareTo(TimeUtils.changeOneDateLastTime(TimeUtils.nowDate()))!=1){
                throw new BusException("设置日期必须大于当前日期！");
            }
            DelayChangeTeacher delayChangeTeacher = new DelayChangeTeacher();
            BeanUtils.copyProperties(delayChangeTeacher, delayChangeTeacherVo);
            delayChangeTeacher.setTeacherId(teacherId);
            delayChangeTeacher.setCreateTime(TimeUtils.now());
            delayChangeTeacher.setUpdateTime(TimeUtils.now());
            delayChangeTeacher.setStatus(0);
            delayChangeTeacher.setOperatorId(principal.id);
            delayChangeTeacher.setOperatorName(principal.name);
            delayChangeTeachers.add(delayChangeTeacher);

        }
        if (delayChangeTeachers.size()>0){
            this.deleteByTeacherIdStatus(teacherId,0);
            this.insertBatch(delayChangeTeachers);
        }
    }

    @Override
    public void deleteByTeacherIdStatus(Long teacherId, Integer status) {
        if (teacherId==null || status==null){
            throw new NullPointerException();
        }
        Example<DelayChangeTeacher> example=DelayChangeTeacher.createExample();
        example.createCriteria().andEqualTo("teacherId",teacherId).andEqualTo("status",status);
        this.deleteByExample(example);
    }

    @Override
    public List<DelayChangeTeacher> findByHappenDateStatus(Date happenDate, Integer status) {
        if (happenDate==null || status==null){
            throw new NullPointerException();
        }
        Example<DelayChangeTeacher> example=DelayChangeTeacher.createExample();
        example.createCriteria().andEqualTo("happenDate",happenDate).andEqualTo("status",status);
        return this.findListByExample(example);
    }

    @Override
    public void changeStatusById(Long delayChangeId, Integer status) {
        DelayChangeTeacher delayChangeTeacher=new DelayChangeTeacher();
        delayChangeTeacher.setDelayChangeId(delayChangeId);
        delayChangeTeacher.setStatus(status);
        this.updateSelectiveById(delayChangeTeacher);
    }

    @Override
    public List<DelayChangeTeacher> findByTeacherIdStatus(Long teacherId, Integer status) {
        if (teacherId==null || status==null){
            throw new NullPointerException();
        }
        Example<DelayChangeTeacher> example=DelayChangeTeacher.createExample();
        example.createCriteria().andEqualTo("teacherId",teacherId).andEqualTo("status",status);
        return this.findListByExample(example);
    }
}